<?php
namespace Api\Controller;
use Think\Hook;

class DownloadController extends ApiController {
    protected $config = [
        'ctrl_auth' => false,
        'login_auth' => false,
    ];
    // 下载ue编辑器的文件
    public function ueditor($aid='') {
        $aid = think_decrypt($aid);
        $model = D('Common/File');
        $param = array(
            'callback'=>null,
            'args'=>null,
            'map'=>array('id'=>$aid, 'from'=>'ueditor'),
        );
        Hook::listen('UEDITOR_FILE_DOWNLOAD', $param);
        if(!$aid) {
            $this->error('参数错误');
        }else if(!$model->where($param['map'])->count()) {
            $this->error('未找到该文件');
        }
        $res = $model->download($aid, $param['callback'], $param['args']);
        if($res===false) {
            $this->error($model->getError());
        }
    }
    public function index() {
        $query = I('request.');
        $aid = $query['aid'];
        $model = D('Common/File');
        $row = $model->find($aid);
        $param = array(
            'callback'=>null,
            'args'=>null,
        );
        Hook::listen('CUSTOM_FILE_DOWNLOAD', $param);
        $res = $model->downLocalFile($row, $param['callback'], $param['args']);
        if($res===false) {
            $this->error($model->getError());
        }
    }
}
